home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / sharware / backup / backup.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  7.8 KB  |  315 lines

  1. {
  2.  Designer: Craig Ward, 100554.2072@compuserve.com
  3.  Date:     22/11/95
  4.  Version:  3.0
  5.  
  6.  
  7.  Function: Backup dialog DLL. User specificies the source and destination directories,
  8.            then the dialog will copy all files.
  9.  
  10.  
  11.  Calling:  NOTE THAT THERE IS A CHANGE IN CALLING THIS DLL. The new call is:
  12.  
  13.             procedure ShowBackupDlg(pSource,pDestination,pHelp,pCompat: pChar; iCompat: longint); far;
  14.             external 'back';
  15.  
  16.  
  17.  Update:   The DLL now copies files using a completely different routine (the
  18.            previous routine used the WinAPI function LZCopy, which though so easy
  19.            to use, it had the drawback that it would expand files that had been
  20.            compressed using the Microsoft compression utility - clearly it's
  21.            meant for setup routines).
  22.  
  23.            Other changes are purely cosmetic, though there is the significant
  24.            addition of confirmation when over-writing existing files.
  25.  
  26.            Also, there are two additions to the parameters passed in calling the DLL.
  27.            Neither of these are used at present, so the new pChar parameter can be set
  28.            to nil, and the new longint parameter set to zero.
  29. *********************************************************************************}
  30. unit Backup;
  31.  
  32. interface
  33.  
  34. uses
  35.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  36.   Forms, Dialogs, StdCtrls, Buttons, FileCtrl, Gauges, ExtCtrls;
  37.  
  38. type
  39.   TBackupDlg = class(TForm)
  40.     DirList: TDirectoryListBox;
  41.     FList: TFileListBox;
  42.     Label1: TLabel;
  43.     lblSource: TLabel;
  44.     Label2: TLabel;
  45.     lblDestination: TLabel;
  46.     btnOK: TBitBtn;
  47.     btnCancel: TBitBtn;
  48.     Panel1: TPanel;
  49.     Gauge1: TGauge;
  50.     BitBtn1: TBitBtn;
  51.     driveBox: TDriveComboBox;
  52.     Bevel1: TBevel;
  53.     Bevel2: TBevel;
  54.     SpeedButton1: TSpeedButton;
  55.     Bevel3: TBevel;
  56.     Bevel4: TBevel;
  57.     procedure btnCancelClick(Sender: TObject);
  58.     procedure btnOKClick(Sender: TObject);
  59.     procedure BitBtn1Click(Sender: TObject);
  60.     procedure btnOKKeyPress(Sender: TObject; var Key: Char);
  61.     procedure SpeedButton1Click(Sender: TObject);
  62.   private
  63.     { Private declarations }
  64.     procedure SetUpFiles;
  65.     procedure CustCopyFiles(sSrce, sDest: string;iNum: integer);
  66.   public
  67.     { Public declarations }
  68.   end;
  69.  
  70. var
  71.   BackupDlg: TBackupDlg;
  72.   iHlp: integer;
  73.  
  74. const
  75.  iHelp: integer = 105; {help-context for SelectDirectory Dialog}
  76.  
  77.  
  78. {exported procedure}
  79. procedure ShowBackupDlg(pSource,pDestination,pHelp,pCompat: pChar; iCompat: longint); export;
  80.  
  81. implementation
  82.  
  83. {$R *.DFM}
  84. {***Exported Procedure**********************************************************}
  85. procedure ShowBackupDlg(pSource,pDestination,pHelp,pCompat: pChar; iCompat: longint);
  86. begin
  87.  {create dialog}
  88.  try
  89.   BackupDlg := TBackupDlg.Create(application);
  90.  
  91.   {set environment}
  92.   if Assigned(pSource) then
  93.    BackupDlg.lblSource.Caption := strPAS(pSource);
  94.   if Assigned(pDestination) then
  95.    BackupDlg.lblDestination.Caption := strPAS(pDestination);
  96.   if Assigned(pHelp) then
  97.    Application.HelpFile := strPAS(pHelp);
  98.  
  99.   {check directories}
  100.   if directoryExists(backupDlg.lblSource.caption) then
  101.    begin
  102.     BackupDlg.FList.Directory := BackupDlg.lblSource.caption;
  103.     BackupDlg.DirList.Directory := BackupDlg.lblSource.Caption;
  104.     BackupDlg.DriveBox.Drive := BackupDlg.DirList.Drive;
  105.    end
  106.   else
  107.     messageDlg('Source Directory not found.',mtWarning,[mbOK],0);
  108.   if not directoryExists(backupDlg.lblDestination.caption) then
  109.    messageDlg('Destination Directory not found.',mtWarning,[mbOK],0);
  110.  
  111.   BackupDlg.ShowModal;
  112.  finally
  113.   BackupDlg.Free;
  114.  end;
  115. end;
  116.  
  117. {***Buttons*********************************************************************}
  118. {help}
  119. procedure TBackupDlg.BitBtn1Click(Sender: TObject);
  120. begin
  121.  Application.HelpCommand(HELP_CONTEXT,BackupDlg.HelpContext);
  122. end;
  123.  
  124.  
  125. {close}
  126. procedure TBackupDlg.btnCancelClick(Sender: TObject);
  127. begin
  128.  close;
  129. end;
  130.  
  131. {copy}
  132. procedure TBackupDlg.btnOKClick(Sender: TObject);
  133. begin
  134.  SetUpFiles;
  135. end;
  136.  
  137. {Select Directory}
  138. procedure TBackupDlg.SpeedButton1Click(Sender: TObject);
  139. var
  140.  sDir: string;
  141. begin
  142.  sDir := lblDestination.Caption;
  143.  if SelectDirectory(sDir,[sdAllowCreate,sdPerformCreate,sdPrompt],iHelp) then
  144.   lblDestination.caption := sDir;
  145. end;
  146.  
  147.  
  148. {***Copy procs******************************************************************}
  149.  
  150. {setup copying}
  151. procedure TBackupDlg.SetUpFiles;
  152. var
  153.  OkToAll: boolean;
  154.  iNum,iGauge: integer;
  155.  sSrce, sDest: ^string;
  156. begin
  157.  try
  158.   New(sSrce);
  159.   New(sDest);
  160.  
  161.   {initialise}
  162.   OkToAll := false;
  163.   iNum := 0;
  164.   iGauge := 0;
  165.  
  166.  {ensure that source directory exists}
  167.  if not directoryExists(lblSource.caption) then
  168.   begin
  169.    messageDlg('Source Directory not found.',mtWarning,[mbOK],0);
  170.    exit;
  171.   end;
  172.  
  173.  {ensure that destination directory exists}
  174.  if not directoryExists(lblDestination.caption) then
  175.   begin
  176.    messageDlg('Destination Directory not found.',mtWarning,[mbOK],0);
  177.    exit;
  178.   end;
  179.  
  180.  {check that the user is not trying to copy over source files}
  181.  if CompareStr(lblSource.Caption,lblDestination.Caption) = 0 then
  182.   begin
  183.    messageDlg('Can not overwrite source files.',mtWarning,[mbOK],0);
  184.    exit;
  185.   end;
  186.  
  187.  {ensure that there are items in the file-list box}
  188.  if (FList.Items.Count) = 0 then
  189.   begin
  190.    messageDlg('No files to be copied.',mtWarning,[mbOK],0);
  191.    exit;
  192.   end;
  193.  
  194.  
  195.  {now, safe to continue with copy...}
  196.  
  197.  {calc progress to add to gauge}
  198.  iGauge := 100 div (FList.Items.Count);
  199.  Panel1.Visible := True;
  200.  
  201.  {init for loop}
  202.  for iNum := 0 to (FList.Items.Count -1) do
  203.   begin
  204.  
  205.   sSrce^ := lblSource.caption + '\' + (ExtractFileName(FList.Items.Strings[iNum]));
  206.   sDest^ := lblDestination.caption + '\' + (ExtractFileName(FList.Items.Strings[iNum]));
  207.  
  208.  {check to see if file exists}
  209.  if not OkToAll then
  210.   begin
  211.   if FileExists(sDest^) then
  212.    begin
  213.     case messageDlg('Overwrite '+sDest^,mtConfirmation,[mbYes,mbAll,mbNo],0) of
  214.  
  215.       idYes:
  216.        custCopyFiles(sSrce^,sDest^,iNum);
  217.  
  218.       (idNo+1): {mrAll}
  219.        begin
  220.         OkToAll := true;
  221.         custCopyFiles(sSrce^,sDest^,iNum);
  222.        end;
  223.  
  224.       idNo:
  225.        {do nothing}
  226.  
  227.     end;
  228.    end
  229.   else
  230.    {file doesn't already exist - so copy}
  231.    custCopyFiles(sSrce^,sDest^,iNum);
  232.   end
  233.   else
  234.    {file does already exist, but overwrite is true}
  235.    custCopyFiles(sSrce^,sDest^,iNum);
  236.  
  237.   {update gauge}
  238.    Gauge1.AddProgress(iGauge);
  239.    Application.ProcessMessages;
  240.  
  241.   end;
  242.  
  243.  {cleanup}
  244.  Panel1.Visible := False;
  245.  Gauge1.Progress := 0;
  246.  OkToAll := false;
  247.  
  248.  finally
  249.   Dispose(sSrce);
  250.   Dispose(sDest);
  251.  end;
  252.  
  253. end;
  254.  
  255.  
  256. {copy routine}
  257. procedure TBackupDlg.CustCopyFiles(sSrce,sDest: string;iNum: integer);
  258. var
  259.  fSrce, fDest: file;
  260.  wRead, wWritten: word;
  261.  p: array[1..2048] of char;
  262. begin
  263.  
  264.   {initialise}
  265.   wRead := 0;
  266.   wWritten := 0;
  267.  
  268.   {assign and open files}
  269.   AssignFile(fSrce,sSrce);
  270.   AssignFile(fDest,sDest);
  271.  
  272.   {$I-}
  273.   Reset(fSrce,1);
  274.   {$I+}
  275.   if IOResult <> 0  then
  276.    begin
  277.     messageDlg('Could not open: '+sSrce,mtWarning,[mbOK],0);
  278.     exit;
  279.    end;
  280.  
  281.   {$I-}
  282.   Rewrite(fDest, 1);
  283.   {$I+}
  284.   if IOResult <> 0  then
  285.    begin
  286.     messageDlg('Could not create: '+sDest,mtWarning,[mbOK],0);
  287.     exit;
  288.    end;
  289.  
  290.   {copy loop}
  291.   repeat
  292.    BlockRead(fSrce, p, SizeOf(p), wRead);
  293.    BlockWrite(fDest, p, wRead, wWritten);
  294.   until (wRead = 0) or (wWritten <> wRead);
  295.  
  296.  {clean up}
  297.  System.CloseFile(fSrce);
  298.  System.CloseFile(fDest);
  299.  
  300. end;
  301.  
  302.  
  303. {***Designer's signature***********************************************}
  304. procedure TBackupDlg.btnOKKeyPress(Sender: TObject; var Key: Char);
  305. begin
  306.  if Key = ^J then
  307.   messageDlg('This was designed by Craig Ward. Craig Ward can be reached'+
  308.              ' at 100554.2072@compuserve.com',mtInformation,[mbOK],0);
  309. end;
  310.  
  311. {}
  312. end.
  313.  
  314.  
  315.